package com.jobcn.controller;

/**
 * 操作任务的Controller,作测试用,实际使用中由商库网维护后台操作
 * Created by winson on 11/02/2017.
 */

import com.jobcn.dic.SpiderDic;
import com.jobcn.entity.SpiderTask;
import com.jobcn.repository.spider.SpiderTaskRepository;
import com.jobcn.service.SpiderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class PublicController {

    @Autowired
    private SpiderService spiderService;
    @Autowired
    private SpiderTaskRepository spiderTaskRepository;

    /**
     * 插入一条58任务
     *
     * @param taskType 0爬虫 1更新
     * @return
     */
    @RequestMapping("insert58Task")
    @ResponseBody
    public String insert58Task(Integer taskType) {
        SpiderTask spiderTask = new SpiderTask();
        spiderTask.setTriggerType(SpiderDic.TRIGGER_MANUAL);
        spiderTask.setUpdateDatetime(new Date());
        spiderTask.setStatus(SpiderDic.STATUS_WAIT);
        spiderTask.setSource(SpiderDic.SPIDER_SOURCE_58);
        spiderTask.setMsg("手动插入任务");
        spiderTask.setTaskType(taskType);
        spiderTaskRepository.save(spiderTask);
        return "插入一条任务";
    }

    /**
     * 终止58爬虫
     *
     * @return
     */
    @RequestMapping("spider58Stop")
    @ResponseBody
    public String spider58Stop() {
        List<SpiderTask> spiderTaskList = spiderTaskRepository.findAllByStatusAndTaskType(SpiderDic.STATUS_PROCESSING, SpiderDic.TASK_SPIDER);
        for (SpiderTask spiderTask : spiderTaskList) {
            spiderTask.setStatus(SpiderDic.STATUS_CLOSE);
            spiderTask.setUpdateDatetime(new Date());
            spiderTaskRepository.save(spiderTask);
        }
        return "终止爬虫";
    }

    /**
     * 恢复58爬虫
     *
     * @return
     */
    @RequestMapping("spider58Resume")
    @ResponseBody
    public Map spider58Resume() {
        return spiderService.spider58Resume();
    }

}
